<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-button type="success" @click="isdr">新增</el-button>
        <el-button type="warning" @click="updateUser">修改</el-button>
        <el-button type="danger" @click="delUser">删除</el-button>
      </el-col>
      <el-col :span="12">
        <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
          <el-select v-model="select" style="width:130px" slot="prepend" placeholder="请选择">
            <el-option label="姓名" value="name"></el-option>
          </el-select>
          <el-button slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </el-col>
    </el-row>
    <!--  -->
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column fixed type="selection" width="70"></el-table-column>
      <el-table-column type="index" width="70" label="#"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"></el-table-column>
      <el-table-column prop="age" label="年龄" width="160"></el-table-column>
      <el-table-column prop="gender" label="性别" width="160"></el-table-column>
      <el-table-column prop="loginName" label="用户名" width="160"></el-table-column>
      <el-table-column prop="loginKey" label="密码" width="300"></el-table-column>
      <el-table-column prop="avatar" label="头像" width="300"></el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row,'删除')" type="text" size="small">删除</el-button>
          <el-button @click="handleClick(scope.row,'修改')" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="400"
    ></el-pagination>
    <!--  -->
    <el-drawer :title="title" :visible.sync="drawer" :before-close="handleClose">
      <div class="demo-drawer__content">
        <el-form :model="form">
          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth">
            <el-input v-model="form.age" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="性别" :label-width="formLabelWidth">
            <el-radio-group v-model="form.gender">
              <el-radio label="男" value="男"></el-radio>
              <el-radio label="女" value="女"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="用户名" :label-width="formLabelWidth">
            <el-input v-model="form.loginName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input v-model="form.loginKey" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div>
          <mavon-editor v-model="form.avatar" @imgAdd="$imgAdd" />
        </div>
        <div class="demo-drawer__footer">
          <el-button @click="cancelForm('取消')">取 消</el-button>
          <el-button type="primary" @click="cancelForm('确定')">确 定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import axios from "axios";
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      title: "新增",
      drawer: false,
      multipleSelection: [],
      input: "",
      select: "",
      currentPage: 1,
      form: {},
      formLabelWidth: "80px",
      pageSize: 10,
      currentPage: 1
    };
  },
  mounted() {
    this.GetUser();
  },
  methods: {
    ...mapActions("users", ["getData", "addData", "editData", "delData"]),
    isdr() {
      this.form = {};
      this.drawer = true;
      this.title = "新增";
      console.log(this.form);
    },
    GetUser() {
      this.getData({ pageSize: this.pageSize, page: this.currentPage - 1 });
    },
    updateUser() {
      if (this.multipleSelection.length != 1) {
        return;
      } else {
        let row = this.multipleSelection[0];
        this.form = row;
        this.drawer = true;
        this.title = "修改";
      }
    },
    delUser() {
      if (this.multipleSelection.length == 0) {
        alert("选择删除项");
        return;
      }
    },
    delete(id) {
      this.delData(id);
    },
    handleClick(row, v) {
      console.log(row, v);
      if (v == "修改") {
        this.form = row;
        this.drawer = true;
        this.title = "修改";
      } else {
        console.log(row.id);
        this.delete(row.id);
      }
      this.GetUser();
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      // console.log(val);
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.GetUser();
      // console.log(`当前页: ${val}`);
    },
    handleClose(done) {
      (this.form = {}), (this.title = "新增");
      done();
    },
    async cancelForm(v) {
      let row = this.form;
      if (v == "确定") {
        if (this.title == "修改") {
          await this.editData(this.form);
        } else {
          await this.addData(row);
        }
        this.GetUser();
        this.form = {};
      }
      this.drawer = false;
    },
    $imgAdd(pos, $file) {
      // 第一步.将图片上传到服务器.
      var formdata = new FormData();
      formdata.append("image", $file);
      console.log(axios);
      axios({
        url: "server url",
        method: "post",
        data: formdata,
        headers: { "Content-Type": "multipart/form-data" }
      }).then(url => {
        // 第二步.将返回的url替换到文本原位置![...](./0) -> ![...](url)
        /**
         * $vm 指为mavonEditor实例，可以通过如下两种方式获取
         * 1. 通过引入对象获取: `import {mavonEditor} from ...` 等方式引入后，`$vm`为`mavonEditor`
         * 2. 通过$refs获取: html声明ref : `<mavon-editor ref=md ></mavon-editor>，`$vm`为 `this.$refs.md`
         */
        $vm.$img2Url(pos, url);
      });
    }
  },
  computed: {
    ...mapState("users", ["data"]),
    tableData() {
      return this.data;
    }
  }
};
</script>

<style scoped>
.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>